package mhserver;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Logger;

import mh.chat.MessageType;
import mh.message.IncomingMessage;
import mh.message.MessageParser;
import mh.util.SHAHash;

public class MessageExecutor implements Runnable {
	/**
	 * The Logger
	 */
	private static final Logger						LOG		= Logger.getLogger(MessageExecutor.class.getName());
	private boolean									running	= false;

	private Pair<String, ClientListener>			receivedMsg;
	private final MagicHogwartsServer				father;

	private final Hashtable<ClientListener, Player>	clientPlayerHashtable;

	public MessageExecutor(MagicHogwartsServer server) {
		this.father = server;
		this.clientPlayerHashtable = this.father.getClientPlayerHashtable();
	}

	@Override
	public void run() {
		this.running = true;
		while (this.running) {

			try {
				this.receivedMsg = this.father.getMessage();
				ClientListener cl = null;
				IncomingMessage incomingMsg = null;

				if (this.receivedMsg != null) {
					String message = this.receivedMsg.getA();
					cl = this.receivedMsg.getB();

					// ****** DEBUG ****** //
					LOG.fine("[MHServer] Analyzing message " + message);
					// ******************* //

					incomingMsg = MessageParser.parseMessage(message);
					Player player = this.clientPlayerHashtable.get(cl);

					this.commandInterpreter(incomingMsg, player, cl);

				}
			} catch (InterruptedException e1) {
				System.out.println("[MHServer] MessageListener has been interrupted");
				this.running = false;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	private void commandInterpreter(IncomingMessage incomingMsg, Player player, ClientListener cl) {
		MessageType mt = MessageType.valueOf(incomingMsg.getCommand());
		switch (mt) {
			case NICK: {
				LOG.config("Nick ricevuto");
				//take salt from database
				String secondsalt = "";
				try {
					SecureRandom sr = new SecureRandom();
					secondsalt = SHAHash.digest("" + (sr.nextDouble() * 1000000000));
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				cl.sendMsg(":server LOGIN salt " + secondsalt);
				break;
			}
			case PASS: {
				ArrayList<String> par = incomingMsg.getParams();
				if ("b7e245a8122f68eb09303a88e3c02e8ccfc7510a18c0a31ebc26c828dd894864".equals(par.get(0))) {
					System.out.println("CAZZOOOOO FUNZIA");
				}
				cl.sendMsg(":server WELCOME " + incomingMsg.getPrefix());
				cl.sendMsg(":server PING server");
				break;
			}
			case MOVE: {
				//TODO check if the player can move and answer
				System.out.println("Move command received from player:" + player.getNickname()); //debug
				break;
			}
			case CHAT: {
				//TODO send a private message to another player
				System.out.println("Chat command received from player:" + player.getNickname()); //debug
				break;
			}
			case BROADCAST: {
				//TODO send a message to all the people in the map
				System.out.println("Broadcast command received from player:" + player.getNickname()); //debug
				break;
			}

			case PING: {
				// Servers should not respond to PING commands but rely on PINGs from
				// the other end of the connection to indicate the connection is alive.
				break;
			}
			default: {
				//XXX if you are here you have done something nasty!
				System.out.println("No recognised command received from player:" + player.getNickname()); //debug
			}
		}
	}

}
